home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* mountain.c - Draws a fractal mountain.
- * Adapted from code written by Benedikt Kessler, SGI Munich
- */
-
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <math.h>
-
- /* Function Prototypes */
-
- GLvoid computeMountain( GLvoid );
- GLvoid drawMountain( GLvoid );
- GLvoid setView( GLvoid );
-
- #define SIZE 128 /* Size means the size of the matrix */
- #define SIZE1 (SIZE + 1)
- #define MEMSIZE ((SIZE1*(SIZE1+1))>>1)
- #define LOG2SIZE 7
-
- #define p(i,j) ( p[ SIZE1*(i) - ((i)*((i)-1) >> 1) + (j) ] )
- #define n(i,j) ( n[ SIZE1*(i) - ((i)*((i)-1) >> 1) + (j) ] )
-
- /* The triangle matrix used has following gestalt:
- * ------
- * | /
- * | /
- * | /
- * | /
- * |/
- *
- * To save storage it's linearizised. Therefore you need the ugly
- * macros defined above.
- *
- * The matrix p[] holds the high values of each vertex of the mountain,
- * n[] holds the normal vectors.
- *
- */
-
- /* private variables */
- static short p[MEMSIZE]; /* Contains highlevel values for mountain */
- static float n[MEMSIZE][3];
- static float look_x,look_y,look_z; /* Position of eye point for glulookat */
-
- static float s_params[]= { 0.0, 0.0, 0.0, 0.0 };
- static float t_params[]= { 0.0, 1.0/70.0, 0.0, 0.3 };
-
- GLvoid
- drawMountain(GLvoid)
- {
- static float v[3];
- register long i=0,j=0,k=0;
-
-
- glPushMatrix();
- gluLookAt(look_x,look_y,look_z,
- SIZE/2.0,-SIZE/3.0,SIZE/2.0,
- 0,1,0);
- v[0] = 0.0;
- v[1] = 1.0;
- v[2] = 0.0;
-
- glNormal3fv(&(n(i,j)[0]));
- for(i=0; i<SIZE; i++)
- {
- glBegin( GL_TRIANGLE_STRIP );
- for(j=SIZE-i;j>0;j--)
- {
- v[0]=(float)i;
- v[1]=(float)p(i,j)/100.0;
- v[2]=(float)j;
- glNormal3fv(n(i,j));
- glVertex3fv(v);
- #ifdef DEBUG
- fprintf(stdout,
- "v[%d][%d] = (%f, %f, %f)\n",
- i,j,v[0],v[1],v[2]);
- #endif
-
- v[0]=(float)i+1;
- v[1]=(float)p(i+1,j-1)/100.0;
- v[2]=(float)j-1;
- glVertex3fv(v);
- }
- v[0]=i;
- v[1]=p(i,0)/100.0; v[2]=0.0;
- glVertex3fv(v);
- glEnd();
- }
-
- glPopMatrix();
- }
-
- GLvoid
- setView( GLvoid )
- {
- look_x = -16.0;
- look_y = 192.0;
- look_z = -16.0;
- }
-
- GLvoid
- computeMountain( GLvoid )
- {
- register long k,i,j,w2,b,w;
- float br;
- float d=.5,e=1;
- float ay, by, nx, nz, len;
-
- w=SIZE;
- for (i=0; i<MEMSIZE; i++)
- p[i]=0;
- p(SIZE/9,SIZE/3)=120;
- for(k=0; k<LOG2SIZE; k++)
- {
- br=e*w*100; w2=w>>1;
-
- for(j=0;j<SIZE;j+=w)
- {
- for(i=0;i<SIZE-j;i+=w)
- {
- b=(p(i,j)+p(i+w,j))>>1;
- p(i+w2,j)+=(b+(rand()/32768.0-d)*br);
- b=(p(j,i)+p(j,i+w))>>1;
- p(j,i+w2)+=(b+(rand()/32768.0-d)*br);
- b=(p(SIZE-j-i,i)+p(SIZE-j-i-w,i+w))>>1;
- p(SIZE-j-i-w2,i+w2)+=(b+(rand()/32768.0-d)*br);
- }
- }
- w>>=1;
- }
-
- /* Now compute normals */
- for(i=0; i<SIZE; i++)
- {
- for(j=SIZE-i;j>0;j--)
- {
- ay = p(i,j-1)-p(i,j);
- by = p(i+1,j-1)-p(i,j);
- nx = -ay; nz = ay-by;
- len = sqrtf(nx*nx + 1.0f + nz*nz);
- n(i,j)[0] = nx/len;
- n(i,j)[1] = -1.0f/len;
- n(i,j)[2] = nz/len;
- }
- }
- }
-